<template>
  <div>
    <!-- versionnum:{{ versionnum }} applynumber:{{ applynumber }} myid:{{ myid }} -->
    <el-form ref="postForm" :model="postForm" class="form-container" :rules="rules">
      <div v-if="readOnly">
        <h3 style="text-align:right">报告编号：{{ postForm.srptBaogbh }}</h3>
        <h1 style="text-align:center">{{ postForm.srptBgmc }}</h1>
        <h3 style="text-align:center">（{{ cplxList.find(item=>item.value===parseInt(postForm.srptQa067))?cplxList.find(item=>item.value===parseInt(postForm.srptQa067)).label:'' }}）</h3>
        <table class="zbgtable">
          <tr>
            <th width="200">产品名称</th>
            <td>{{ postForm.srptYpmc }}</td>
          </tr>
          <tr>
            <th width="200">产品型号</th>
            <td>{{ postForm.sggxh }}</td>
          </tr>
          <tr>
            <th width="200">委托单位</th>
            <td>{{ postForm.srptWtdw }}</td>
          </tr>
          <tr>
            <th width="200">检验类别</th>
            <td>{{ postForm.srptJylb }}</td>
          </tr>
        </table>
      </div>
      <table v-else class="zbgtable">
        <tr>
          <th width="200">报告编号</th>
          <td align="left">
            <div v-if="myid">{{ postForm.srptBaogbh }}</div>
            <el-form-item v-else prop="srptBaogbh">
              <el-input v-model.trim="postForm.srptBaogbh" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">报告名称</th>
          <td align="left">
            <el-form-item prop="srptBgmc">
              <el-input v-model.trim="postForm.srptBgmc" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">车辆类型</th>
          <td align="left">{{ cplxList.find(item=>item.value===parseInt(postForm.srptQa067))?cplxList.find(item=>item.value===parseInt(postForm.srptQa067)).label:'' }}</td>
        </tr>
        <tr>
          <th width="200">产品名称</th>
          <td align="left">
            <el-form-item prop="srptYpmc">
              <el-input v-model.trim="postForm.srptYpmc" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">产品型号</th>
          <td align="left">
            <el-form-item prop="sggxh">
              <el-input v-model.trim="postForm.sggxh" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">委托单位</th>
          <td align="left">
            <el-form-item prop="srptWtdw">
              <el-input v-model.trim="postForm.srptWtdw" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">检验类别</th>
          <td align="left">
            <el-form-item prop="srptJylb">
              <el-input v-model.trim="postForm.srptJylb" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
      </table>
      <div>
        <el-button v-if="pdfId" type="" style="margin-bottom:10px;" @click="viewFile(pdfId)">查看报告PDF文件</el-button>
        <el-upload
          v-if="!readOnly"
          class="upload-demo"
          style="margin-bottom:10px;"
          :action="this.$http.defaults.baseURL+'api/file/upload/'"
          :on-success="handleUploadSucc"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :on-error="uploadError"
          :multiple="false"
          :file-list="fileList"
          :headers="myHeaders"
          :limit="1"
          :on-exceed="handleExceed"
          :before-upload="beforeUpload"
          accept="application/pdf"
          :on-preview="handlePreview"
        >
          <el-button type="primary">{{ pdfId?"重新上传报告PDF文件":"点击上传报告PDF文件" }}</el-button>
        </el-upload>
      </div>
      <table class="zbgtable">
        <tr>
          <th width="200">检验单位</th>
          <td v-if="readOnly">{{ postForm.srptJydw }}</td>
          <td v-else align="left">
            <el-form-item prop="srptJydw">
              <el-input v-model.trim="postForm.srptJydw" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">地址</th>
          <td v-if="readOnly">{{ postForm.srptJydwDz }}</td>
          <td v-else align="left">
            <el-form-item prop="srptJydwDz">
              <el-input v-model.trim="postForm.srptJydwDz" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">电话</th>
          <td v-if="readOnly">{{ postForm.srptJydwDh }}</td>
          <td v-else align="left">
            <el-form-item prop="srptJydwDh">
              <el-input v-model.trim="postForm.srptJydwDh" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">传真</th>
          <td v-if="readOnly">{{ postForm.srptJydwCz }}</td>
          <td v-else align="left">
            <el-form-item prop="srptJydwCz">
              <el-input v-model.trim="postForm.srptJydwCz" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">邮政编码</th>
          <td v-if="readOnly">{{ postForm.srptJydwYb }}</td>
          <td v-else align="left">
            <el-form-item prop="srptJydwYb">
              <el-input v-model.trim="postForm.srptJydwYb" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
      </table>
      <table class="zbgtable">
        <tr>
          <th width="200">委托单位</th>
          <td>{{ postForm.srptWtdw }}</td>
        </tr>
        <tr>
          <th width="200">地址</th>
          <td v-if="readOnly">{{ postForm.srptWtdwDz }}</td>
          <td v-else align="left">
            <el-form-item prop="srptWtdwDz">
              <el-input v-model.trim="postForm.srptWtdwDz" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">电话</th>
          <td v-if="readOnly">{{ postForm.srptWtdwDh }}</td>
          <td v-else align="left">
            <el-form-item prop="srptWtdwDh">
              <el-input v-model.trim="postForm.srptWtdwDh" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">传真</th>
          <td v-if="readOnly">{{ postForm.srptWtdwCz }}</td>
          <td v-else align="left">
            <el-form-item prop="srptWtdwCz">
              <el-input v-model.trim="postForm.srptWtdwCz" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">邮政编码</th>
          <td v-if="readOnly">{{ postForm.srptWtdwYb }}</td>
          <td v-else align="left">
            <el-form-item prop="srptWtdwYb">
              <el-input v-model.trim="postForm.srptWtdwYb" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
      </table>
      <table class="zbgtable">
        <tr>
          <th width="200">样品名称</th>
          <td v-if="readOnly" width="400">{{ postForm.srptYpmc }}</td>
          <td v-else width="400">
            {{ postForm.srptYpmc }}
          </td>
          <th width="200">商标</th>
          <td v-if="readOnly" width="400">{{ postForm.srptSb }}</td>
          <td v-else width="400">
            <el-form-item prop="srptSb">
              <el-input v-model.trim="postForm.srptSb" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">型号规格</th>
          <td v-if="readOnly" width="400">{{ postForm.sggxh }}</td>
          <td v-else width="400">
            {{ postForm.sggxh }}
          </td>
          <th width="200">检验类别</th>
          <td v-if="readOnly" width="400">{{ postForm.srptJylb }}</td>
          <td v-else width="400">
            {{ postForm.sggxh }}
          </td>
        </tr>
        <tr>
          <th width="200">受检单位</th>
          <td v-if="readOnly" width="400">{{ postForm.srptSjdw }}</td>
          <td v-else width="400">
            <el-form-item prop="srptSjdw">
              <el-input v-model.trim="postForm.srptSjdw" style="width:610px" />
            </el-form-item>
          </td>
          <th width="200">生产单位</th>
          <td v-if="readOnly" width="400">{{ postForm.srptCompany }}</td>
          <td v-else width="400">
            <el-form-item prop="srptCompany">
              <el-input v-model.trim="postForm.srptCompany" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">送样者</th>
          <td v-if="readOnly" width="400">{{ postForm.srptSender }}</td>
          <td v-else width="400">
            <el-form-item prop="srptSender">
              <el-input v-model.trim="postForm.srptSender" style="width:610px" />
            </el-form-item>
          </td>
          <th width="200">送样日期</th>
          <td v-if="readOnly" width="400">{{ timestampToTime(postForm.srptSendDate) }}</td>
          <td v-else width="400">
            <el-form-item prop="srptSendDate">
              <el-date-picker v-model="postForm.srptSendDate" type="date" value-format="yyyy-MM-dd" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">样品数量</th>
          <td v-if="readOnly" width="400">{{ postForm.srptYpslOne }}</td>
          <td v-else width="400">
            <el-form-item prop="srptYpslOne">
              <el-input-number v-model.trim="postForm.srptYpslOne" :precision="0" :min="0" />
            </el-form-item>
          </td>
          <th width="200">生产日期</th>
          <td v-if="readOnly" width="400">{{ timestampToTime(postForm.srptScrq) }}</td>
          <td v-else width="400">
            <el-form-item prop="srptScrq">
              <el-date-picker v-model="postForm.srptScrq" type="date" value-format="yyyy-MM-dd" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">检验依据</th>
          <td v-if="readOnly" width="400">{{ postForm.jyyjXxx }}</td>
          <td v-else width="400">
            {{ postForm.jyyjXxx }}
          </td>
          <th width="200">检验项目</th>
          <td v-if="readOnly" width="400">{{ postForm.jyxmXxx }}</td>
          <td v-else width="400">
            {{ postForm.jyxmXxx }}
          </td>
        </tr>
        <tr>
          <th>检验结论</th>
          <td v-if="readOnly" colspan="3" style="text-align:left">{{ postForm.srptJyjl }}<p style="text-align:right">{{ postForm.drptQfrq }}</p></td>
          <td v-else colspan="3" style="text-align:left">
            <el-form-item prop="srptJlpd">
              <el-input v-model.trim="postForm.srptJlpd" style="width:610px" />
            </el-form-item>
            <el-form-item prop="drptQfrq">
              <el-date-picker v-model="postForm.drptQfrq" type="date" value-format="yyyy-MM-dd" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th>备注</th>
          <td v-if="readOnly" colspan="3" style="text-align:left">{{ postForm.sbz }}</td>
          <td v-else colspan="3" style="text-align:left">
            <el-form-item prop="sbz">
              <el-input v-model.trim="postForm.sbz" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
      </table>
      <div v-if="readOnly" style="text-align:center">批准：{{ postForm.spzry }} 审核：{{ postForm.sshry }} 主检：{{ postForm.szyry }}</div>
      <table v-else class="zbgtable">
        <tr>
          <th width="200">批准人员</th>
          <td>
            <el-form-item prop="spzry">
              <el-input v-model.trim="postForm.spzry" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">审核人员</th>
          <td>
            <el-form-item prop="sshry">
              <el-input v-model.trim="postForm.sshry" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">主检人员</th>
          <td>
            <el-form-item prop="szyry">
              <el-input v-model.trim="postForm.szyry" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
      </table>
      <div>
        <h3>一、检验结果</h3>
        <div>
          <el-table
            ref="processTable"
            :data="repResultInfo"
            border
            fit
            highlight-current-row
            style="width: 100%;margin-bottom:20px;"
          >
            <el-table-column prop="rNumber" label="序号" width="60" align="left" />
            <el-table-column prop="rNamenum" label="检验项目" width="250" align="left" />
            <el-table-column prop="rStandard" label="标准要求" min-width="200" align="left" />
            <el-table-column prop="rType" label="类型" width="200" align="left" />
            <el-table-column prop="rResultcode" label="检验结果" width="400" align="left">
              <template slot-scope="scope">
                <span v-if="readOnly">{{ scope.row.rName }}{{ scope.row.resultValue }}</span>
                <el-input v-else v-model="scope.row.resultValue" style="width:260px" @input="change(scope.$index,repResultInfo)" @blur="change(scope.$index,repResultInfo)" />
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <h3>二、检验时间、地点</h3>
      <div v-if="readOnly">检验于 {{ postForm.sjysjS }} 至 {{ postForm.sjysjE }} 在 {{ postForm.ssycd }} 进行。</div>

      <table v-else class="zbgtable">
        <tr>
          <th width="200">检验开始时间</th>
          <td>
            <el-form-item prop="sjysjS">
              <el-date-picker v-model="postForm.sjysjS" type="date" value-format="yyyy-MM-dd" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">检验结束时间</th>
          <td>
            <el-form-item prop="sjysjE">
              <el-date-picker v-model="postForm.sjysjE" type="date" value-format="yyyy-MM-dd" />
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th width="200">检验地点</th>
          <td>
            <el-form-item prop="ssycd">
              <el-input v-model.trim="postForm.ssycd" style="width:610px" />
            </el-form-item>
          </td>
        </tr>
      </table>
      <div>
        <h3>附录1：样品情况表</h3>
        <el-table
          ref="processTable"
          :data="repApplyInfo"
          border
          fit
          highlight-current-row
          style="width: 100%;margin-bottom:20px;"
        >
          <el-table-column prop="aName" label="项目" width="300" align="left" />
          <el-table-column prop="aNamenum" label="样品情况" min-width="400" align="center">
            <template slot-scope="scope">
              <span v-if="readOnly">{{ scope.row.applyValue }}</span>
              <el-input v-else v-model="scope.row.applyValue" style="width:260px" @input="change(scope.$index,repApplyInfo)" @blur="change(scope.$index,repApplyInfo)" />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div>
        <h3>附录2：试验及样车照片</h3>
        <!-- {{ photoList }} -->
        <div v-for="(item,index) in photoList" :key="index" style="text-align:center">
          <template v-if="item.keyType==='img'">
            <div v-if="!readOnly">
              <el-input v-model="item.context" style="width:400px;" /><el-button type="primary" @click="addText(index,item.context)">添加文字说明</el-button>
              <el-popconfirm title="确定将下方图片删除？" style="margin-left:10px;" @onConfirm="removeItem(index)">
                <el-button slot="reference" type="danger">删除该图片</el-button>
              </el-popconfirm>
            </div>
            <img :src="item.jsonValue" style="margin-bottom:20px;">
          </template>
          <template v-else>
            <el-popconfirm v-if="!readOnly" title="确定将下方说明文字删除？" style="margin-left:10px;" @onConfirm="removeItem(index)">
              <el-button slot="reference" type="danger">删除说明文字</el-button>
            </el-popconfirm>
            <div style="margin-bottom:20px;">{{ item.jsonValue }}</div>
          </template>
        </div>
        <el-upload
          v-if="!readOnly"
          class="upload-demo"
          style="margin-bottom:10px;"
          :action="this.$http.defaults.baseURL+'api/file/upload/'"
          :on-success="handleUploadImgSucc"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :on-error="uploadError"
          :multiple="false"
          :file-list="imgList"
          :headers="myHeaders"
          :limit="1"
          :on-exceed="handleExceed"
          :before-upload="beforeUploadImg"
          accept="image/jpeg,image/png"
        >
          <el-button type="primary">上传图片</el-button>
        </el-upload>
      </div>
      <el-form-item v-if="!readOnly" style="margin:10px 0 0 0">
        <el-button type="primary" :loading="loading" @click="submitForm(0)">暂存</el-button>
        <el-button type="success" :loading="loading" @click="submitForm(1)">生成</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
const defaultForm = {
  'applynumber': '',
  'dRptQfrq': '',
  'drptQfrq': '',
  'sRptScrq': '',
  'sRptSendDate': '',
  'sbz': '',
  'sggxh': '',
  'sjysjE': '',
  'sjysjS': '',
  'spzry': '',
  'srptBaogbh': '',
  'srptBgmc': '',
  'srptCompany': '',
  'srptJlpd': '',
  'srptJydw': '',
  'srptJydwCz': '',
  'srptJydwDh': '',
  'srptJydwDz': '',
  'srptJydwYb': '',
  'srptJyjl': '',
  'srptJylb': '',
  'srptProductmodel': '',
  'srptProductname': '',
  'srptQa003': '',
  'srptQa004': '',
  'srptQa067': 0,
  'srptQrCode': '',
  'srptSb': '',
  'srptScrq': '',
  'srptSendDate': '',
  'srptSender': '',
  'srptSjdw': '',
  'srptWa001': '',
  'srptWtdw': '',
  'srptWtdwCz': '',
  'srptWtdwDh': '',
  'srptWtdwDz': '',
  'srptWtdwYb': '',
  'srptXmmc': '',
  'srptYpmc': '',
  'srptYpslN': 0,
  'srptYpslOne': 0,
  'sshry': '',
  'sstate': 0,
  'ssycd': '',
  'szyry': '',
  'tbaogbh': '',
  'tnamenum': 0,
  'tnametype': '',
  'tnumber': 0,
  'tproductmodel': '',
  'trealnum': 0,
  'tresult': '',
  'tstandard': '',
  'versionnum': ''
}
export default {
  name: 'ArticleDetail',
  props: {
    applynumber: {
      type: String,
      default: ''
    },
    versionnum: {
      type: String,
      default: ''
    },
    readOnly: {
      type: Boolean,
      default: false
    },
    infoid: {
      type: String,
      default: ''
    },
    timestamp: {
      type: Number,
      default: 0
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (!value) {
        // this.$message({
        //   message: '请将表单填写完整',
        //   type: 'error'
        // })
        callback(new Error('必填项'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        srptBaogbh: [{ validator: validateRequire }],
        srptJlpd: [{ validator: validateRequire }]
      },
      imgList: [],
      fileList: [],
      applyJdoc: '',
      pdfId: '',
      photoJson: {},
      resultJdoc: '',
      photoList: [],
      repResultInfo: [],
      repApplyInfo: [],
      myid: '',
      myHeaders: {},
      imageUrl: '',
      infoLoading: false,
      postForm: {},
      loading: false,
      cplxList: [{ label: '客车', value: 1 }, { label: '乘用车', value: 2 }, { label: '载货汽车', value: 4 }, { label: '挂车', value: 5 }, { label: '牵引车', value: 6 }]
    }
  },
  watch: {
    timestamp: {
      handler(val, oldVal) {
        this.mystep = this.step
        this.myid = this.infoid
        if (this.myid) {
          this.getData(this.myid)
        }
        if (this.versionnum) {
          this.getReportConf(this.versionnum)
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.myHeaders = {
      Authorization: localStorage.getItem('token')
    }
  },
  methods: {
    addText(index, text) {
      this.photoList.splice(index, 0, { jsonValue: text, keyType: 'text' })
    },
    removeItem(index) {
      this.photoList.splice(index, 1)
    },
    handlePreview(file) {
      window.open(file.url)
    },
    beforeUpload(file) {
      const isPdf = file.type === 'application/pdf'
      const isLt200M = file.size / 1024 / 1024 < 200

      if (!isPdf) {
        this.$message.error('上传文件只能是 PDF 格式!')
        return false
      }
      if (!isLt200M) {
        this.$message.error('上传文件大小不能超过 200MB!')
        return false
      }
    },
    beforeUploadImg(file) {
      const isPic = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt20M = file.size / 1024 / 1024 < 20

      if (!isPic) {
        this.$message.error('上传文件只能是 jpg 或 png 格式!')
        return false
      }
      if (!isLt20M) {
        this.$message.error('上传文件大小不能超过 20MB!')
        return false
      }
    },
    viewFile(id) {
      window.open(this.$http.defaults.baseURL + 'api/file/download?id=' + id)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`一次最多只能上传一个文件`)
    },
    uploadError(err) {
      console.log(err)
      this.$message({
        message: '上传失败:' + err,
        type: 'error',
        duration: 10000
      })
    },
    handleUploadSucc(response, file, fileList) {
      if (response.success) {
        this.$message({
          type: 'success',
          message: '上传成功!'
        })
        this.pdfId = response.result[0].id
        this.fileList.push({ id: response.result[0].id, name: response.result[0].fileName + '.' + response.result[0].fileType, fileType: response.result[0].fileType, url: this.$http.defaults.baseURL + 'api/file/download?id=' + response.result[0].id, dataCreateDatetime: response.result[0].dataCreateDatetime })
      } else {
        this.$message({
          message: '上传失败:' + response.message,
          type: 'error',
          duration: 10000
        })
      }
    },
    handleUploadImgSucc(response, file, fileList) {
      if (response.success) {
        this.$message({
          type: 'success',
          message: '上传成功!'
        })
        this.imgList = []
        this.photoList.push({ jsonValue: this.$http.defaults.baseURL + 'api/file/download?id=' + response.result[0].id, keyType: 'img' })
      } else {
        this.$message({
          message: '上传失败:' + response.message,
          type: 'error',
          duration: 10000
        })
      }
    },
    beforeRemove(file, fileList) {
      console.log(file)
      const isPdf = file.fileType === 'pdf'
      if (isPdf) {
        return this.$confirm(`确定移除 ${file.name}？`)
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
      const fid = file.id
      this.$http.get('api/file/remove?id=' + fid).then(res => {
        if (res.data.success === true) {
          this.fileList = this.fileList.filter(item => item.id !== fid)
          this.$message({
            message: '删除成功！',
            type: 'success'
          })
        }
      })
    },
    change(index, obj) {
      const tmpObj = obj[index]
      this.$set(obj, index, tmpObj)
    },
    submitForm(sstate) {
      this.$refs.postForm.validate(valid => {
        console.log(valid)
        if (valid) {
          this.applyJdoc = {}
          this.repApplyInfo.filter(item => item.applyValue !== '' && item.applyValue !== null).forEach(item => { this.applyJdoc[item.aNamenum] = item.applyValue })
          this.resultJdoc = {}
          this.repResultInfo.filter(item => item.resultValue !== '' && item.resultValue !== null).forEach(item => {
            this.resultJdoc[item.rResultcode] = item.resultValue
          })
          this.photoJson = {}
          let groupIndex = 1
          let innerIndex = 1
          this.photoList.forEach((item, index) => {
            console.log('item,index')
            console.log(item, index)
            if (index === 0 && item.keyType !== 'text') {
              this.photoJson['spz_1'] = ''
            }
            if (item.keyType === 'text') {
              if (index !== 0) {
                groupIndex++
                innerIndex = 1
              }
              this.photoJson['spz_' + groupIndex] = item.jsonValue
            } else {
              this.photoJson['s_rpt_img_' + groupIndex + '_' + innerIndex] = item.jsonValue
              innerIndex++
            }
            console.log('this.photoJson')
            console.log(JSON.stringify(this.photoJson))
          })
          this.$nextTick(() => {
            const para = {
              applyJdoc: JSON.stringify(this.applyJdoc),
              applynumber: this.applynumber ? this.applynumber : this.postForm.applynumber,
              dRptQfrq: this.postForm.dRptQfrq,
              drptQfrq: this.postForm.drptQfrq,
              pdfId: this.pdfId,
              photoJson: JSON.stringify(this.photoJson),
              resultJdoc: JSON.stringify(this.resultJdoc),
              sRptScrq: this.postForm.sRptScrq,
              sRptSendDate: this.postForm.sRptSendDate,
              sbz: this.postForm.sbz,
              sggxh: this.postForm.sggxh,
              sjysjE: this.postForm.sjysjE,
              sjysjS: this.postForm.sjysjS,
              spzry: this.postForm.spzry,
              srptBaogbh: this.postForm.srptBaogbh,
              srptBgmc: this.postForm.srptBgmc,
              srptCompany: this.postForm.srptCompany,
              srptJlpd: this.postForm.srptJlpd,
              srptJydw: this.postForm.srptJydw,
              srptJydwCz: this.postForm.srptJydwCz,
              srptJydwDh: this.postForm.srptJydwDh,
              srptJydwDz: this.postForm.srptJydwDz,
              srptJydwYb: this.postForm.srptJydwYb,
              srptJyjl: this.postForm.srptJyjl,
              srptJylb: this.postForm.srptJylb,
              srptProductmodel: this.postForm.srptProductmodel,
              srptProductname: this.postForm.srptProductname,
              srptQa003: this.postForm.srptQa003,
              srptQa004: this.postForm.srptQa004,
              srptQa067: this.postForm.srptQa067,
              srptQrCode: this.postForm.srptQrCode,
              srptSb: this.postForm.srptSb,
              srptScrq: this.postForm.srptScrq,
              srptSendDate: this.postForm.srptSendDate,
              srptSender: this.postForm.srptSender,
              srptSjdw: this.postForm.srptSjdw,
              srptWa001: this.postForm.srptWa001,
              srptWtdw: this.postForm.srptWtdw,
              srptWtdwCz: this.postForm.srptWtdwCz,
              srptWtdwDh: this.postForm.srptWtdwDh,
              srptWtdwDz: this.postForm.srptWtdwDz,
              srptWtdwYb: this.postForm.srptWtdwYb,
              srptXmmc: this.postForm.srptXmmc,
              srptYpmc: this.postForm.srptYpmc,
              srptYpslN: this.postForm.srptYpslN,
              srptYpslOne: this.postForm.srptYpslOne,
              sshry: this.postForm.sshry,
              sstate: sstate,
              ssycd: this.postForm.ssycd,
              szyry: this.postForm.szyry,
              tbaogbh: this.postForm.tbaogbh,
              tnamenum: this.postForm.tnamenum,
              tnametype: this.postForm.tnametype,
              tnumber: this.postForm.tnumber,
              tproductmodel: this.postForm.tproductmodel,
              trealnum: this.postForm.trealnum,
              tresult: this.postForm.tresult,
              tstandard: this.postForm.tstandard,
              versionnum: this.versionnum ? this.versionnum : this.postForm.versionnum
            }
            this.loading = true
            const url = this.myid ? '/api/check/updateSubReport' : '/api/check/saveSubReport'
            this.$http
              .post(url, para)
              .then(response => {
                this.loading = false
                if (response.data.success === true) {
                  this.$message({
                    message: sstate === 0 ? '暂存成功' : '生成成功',
                    type: 'success'
                  })
                  this.$emit('dialogHide')
                  this.postForm = Object.assign({}, defaultForm)
                  this.loading = false
                } else {
                  console.log('操作失败:' + response.data.message)
                  this.$message({
                    message: '操作失败:' + response.data.message,
                    type: 'error'
                  })
                }
              })
              .catch(function(error) {
                console.log(error)
                this.loading = false
              })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    applyValue(key) {
      if (this.postForm.applyJdoc) {
        const value = JSON.parse(this.postForm.applyJdoc)[key]
        if (value) {
          return value
        }
      }
    },
    resultValue(key) {
      if (this.postForm.resultJdoc) {
        const value = JSON.parse(this.postForm.resultJdoc)[key]
        if (value) {
          return value
        }
      }
    },
    timestampToTime(timestamp) {
      var date = new Date(timestamp) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      const Y = date.getFullYear() + '-'
      const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      const D = change(date.getDate()) + ' '
      return Y + M + D
      function change(t) {
        if (t < 10) {
          return '0' + t
        } else {
          return t
        }
      }
    },
    showReport(versionnum, col) {
      const report = this.postForm.dreports.find(item => item.versionnum === versionnum)
      if (report) {
        console.log('myreport')
        console.log(report)
        return report[col]
      }
    },
    getData(id) {
      this.infoLoading = true
      if (id) {
        this.$http
          .get('/api/check/getReportByNumber?&reportNumber=' + id)
          .then(response => {
            this.infoLoading = false
            if (response.data.success === true) {
              this.postForm = response.data.result
              this.pdfId = this.postForm.pdfId
              this.fileList = []
              this.getReportConf(this.postForm.versionnum)
              this.photoJson = {}
              this.photoList = []
              if (this.postForm.photoJson) {
                const photoJson = JSON.parse(this.postForm.photoJson)
                console.log(photoJson)
                let i = 1
                while (photoJson['spz_' + i] || photoJson['spz_' + i] === '') {
                  this.photoList.push({ jsonValue: photoJson['spz_' + i], keyType: 'text' })
                  let j = 1
                  while (photoJson['s_rpt_img_' + i + '_' + j]) {
                    this.photoList.push({ jsonValue: photoJson['s_rpt_img_' + i + '_' + j], keyType: 'img' })
                    j++
                  }
                  i++
                }
                // for (const item in JSON.parse(this.postForm.photoJson)) {
                //   console.log(item)
                //   const keyType = item.indexOf('s_rpt_img') > -1 ? 'img' : 'text'
                //   this.photoList.push({ jsonValue: JSON.parse(this.postForm.photoJson)[item], keyType: keyType })
                // }
              }
            } else {
              console.log('获取失败:' + response.data.message)
              this.$message({
                message: '获取失败:' + response.data.message,
                type: 'error'
              })
            }
          })
          .catch(error => {
            console.log(error)
            this.infoLoading = false
          })
      }
    },
    getApplyValue() {
      this.$http
        .get('/api/check/getContentByApplyNumber?applyNumber=' + this.applynumber)
        .then(response => {
          this.infoLoading = false
          if (response.data.success === true) {
            this.repApplyInfo.forEach((item, index) => {
              console.log(response.data.result[item.aNamenum])
              item.applyValue = response.data.result[item.aNamenum]
              this.$set(this.repApplyInfo, index, item)
            })
            this.postForm.srptYpmc = response.data.result.QA001
            this.postForm.sggxh = response.data.result.QA002
            this.postForm.srptSb = response.data.result.QA005
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.infoLoading = false
        })
    },
    getReportConf(versionnum) {
      this.$http
        .get('/api/check/getReportData?version=' + versionnum)
        .then(response => {
          this.infoLoading = false
          if (response.data.success === true) {
            this.repApplyInfo = response.data.result.repApplyInfo
            this.repResultInfo = response.data.result.repResultInfo
            if (this.myid) {
              this.repApplyInfo.forEach(item => {
                item.applyValue = this.applyValue(item.aNamenum)
              })
              this.repResultInfo.forEach(item => {
                item.resultValue = this.resultValue(item.rResultcode)
              })
            } else {
              this.postForm = Object.assign({}, defaultForm)
              this.postForm.srptBgmc = response.data.result.repName
              this.postForm.srptQa067 = response.data.result.cartype
              // 增加其他初始化
              this.getApplyValue()
            }
          } else {
            console.log('获取失败:' + response.data.message)
          }
        })
        .catch(error => {
          console.log(error)
          this.infoLoading = false
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.zbgtable,.zbgtable tr th, .zbgtable tr td { border:1px solid rgb(223, 230, 236)}
.zbgtable { margin-bottom: 20px; width: 100%; min-height: 25px; line-height: 25px; text-align: center; border-collapse: collapse;}
</style>
